#include "loginwindow.h"
#include "ui_loginwindow.h"
#include <QMessageBox>

LoginWindow::LoginWindow(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::LoginWindow)
    , mainWindow(nullptr)
{
    ui->setupUi(this);
    setWindowTitle("登录");

    // 设置密码框显示为密码形式
    ui->passwordEdit->setEchoMode(QLineEdit::Password);

    // 设置固定大小
    setFixedSize(300, 200);
}

LoginWindow::~LoginWindow()
{
    delete ui;
    if (mainWindow) {
        delete mainWindow;
    }
}

void LoginWindow::on_loginButton_clicked()
{
    QString username = ui->usernameEdit->text();
    QString password = ui->passwordEdit->text();

    // 验证用户名和密码
    if (username == "admin" && password == "8") {
        // 登录成功，隐藏登录窗口，显示主窗口
        this->hide();
        if (!mainWindow) {
            mainWindow = new MainWindow();
        }
        mainWindow->show();
    } else {
        QMessageBox::warning(this, "登录失败", "用户名或密码错误！\n请使用admin和密码8登录");
    }
}

void LoginWindow::on_clearButton_clicked()
{
    // 清空输入
    ui->usernameEdit->clear();
    ui->passwordEdit->clear();
    // 焦点设置到用户名输入框
    ui->usernameEdit->setFocus();
}
